package com.GLM.Viewer.Friends;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.GLM.Viewer.R.string;
import com.GLM.Viewer.Users.UserInfoDetailActivity;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

public class FriendsSearchResultActivity extends Activity{
	private final static int FRIENDLIST_LOADING = 111;
	private final static int FRIENDLIST_LONDED = 222;
	ListView friendsListView;
	String searchString;
	private int screenWidth;
	private int screenHeight;
	private List<String> nickNames = new ArrayList<String>();
    private Map<Integer, Integer> friendsMap = new HashMap<Integer, Integer>();
//    private SimpleAdapter mFriendsAdapter;
    private ArrayAdapter<String> mFriendsAdapter;
    private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Thread thread;
			switch (msg.what) {
			case FRIENDLIST_LOADING:
				mFriendsAdapter = null;
				thread = new Thread(new Runnable() {
					public void run() {
						initSearchFriends();
						mFriendsAdapter = new ArrayAdapter<String>(FriendsSearchResultActivity.this, R.layout.view_search_result_item, R.id.search_item_text,  nickNames);
						mHandler.sendEmptyMessage(FRIENDLIST_LONDED);
					}

				});
				thread.start();
				break;

			case FRIENDLIST_LONDED:
				if (mFriendsAdapter != null && nickNames.size() >0) {
					friendsListView.setAdapter(mFriendsAdapter);
					bindItemClick();
					
				}else {
					Paint paint = new Paint();
					paint.setTextSize(20);
					
					String str = getString(R.string.friends_search_nofriends);
					Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.glm_background);
					Canvas canvas = new Canvas(bitmap);
					canvas.drawText(str,screenWidth/2-str.length()*9 , screenHeight/2, paint);
					RelativeLayout layout = (RelativeLayout)findViewById(R.id.friendslayout);
					Drawable drawable = new BitmapDrawable(bitmap);
					layout.setBackgroundDrawable(drawable);
					
				}
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		screenHeight = getWindowManager().getDefaultDisplay().getHeight();
		setContentView(R.layout.view_friends_searchresult);
		
		
		friendsListView = (ListView)findViewById(R.id.search_result_listview);
		Intent intent = getIntent();
		searchString = intent.getStringExtra("SearchStr");
		mHandler.sendEmptyMessage(FRIENDLIST_LOADING);
	}
	
	public void bindItemClick(){
		friendsListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(FriendsSearchResultActivity.this, UserInfoDetailActivity.class);
				intent.putExtra(Constant.INTENT_USER_ID, friendsMap.get(position));
				startActivity(intent);
				
			}
		});
	}
	
	public void initSearchFriends(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			jsonData.put("nickname", searchString);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_SEARCHFRIENDS, jsonData);
			if (jsonResponse.getStatus() != Status.ERR) {
				int status = -1;
				JSONObject data = jsonResponse.getData();
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray(Constant.RESP_NICKNAMEDATA);

					initFriendItemFromJson(jsonArray);
				}

			}
		} catch (Exception e) {
			// TODO: handle exception

		}
	}
	
	public void initFriendItemFromJson(JSONArray jsonArray) throws JsonSyntaxException, JSONException {
		Gson gson = new Gson();
		for (int i = 0; i < jsonArray.length(); i++) {
			FriendsSearchEntity entity = gson.fromJson(jsonArray.getJSONObject(i).toString(), FriendsSearchEntity.class);
			friendsMap.put(i, entity.getUd_user_id());
			nickNames.add(entity.getUd_nickname());
		}
	}
	
	class FriendsSearchEntity{
		private int ud_user_id;
		private String ud_nickname;
		
		public int getUd_user_id() {
			return ud_user_id;
		}
		public void setUd_user_id(int ud_user_id) {
			this.ud_user_id = ud_user_id;
		}
		public String getUd_nickname() {
			return ud_nickname;
		}
		public void setUd_nickname(String ud_nickname) {
			this.ud_nickname = ud_nickname;
		}
		
	}
}
